package com.jinlejia.erp.ui.mine.process;

import android.os.Bundle;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.BaseActivity;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.databinding.ActivityProgressBinding;
import com.jinlejia.erp.ui.mine.adapter.ProcessProAdapter;
import com.jinlejia.erp.utils.DataFilter;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.Map;

import okhttp3.Call;

/**
 * 办件进度（Activity）
 */

public class ProcessProgressActivity extends BaseActivity {
    ArrayList<Map<String, Object>> dataList = new ArrayList<>();

    private ProcessProAdapter adapter;
    private ActivityProgressBinding binding;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_progress);
        initView();
    }

    /**
     * 初始化视图
     */
    private void initView() {
        getData();
        binding.processRecyclerView.setHasFixedSize(true);
        binding.backBtn.setOnClickListener(view -> finish());
        binding.processRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//        binding.processRecyclerView.setNestedScrollingEnabled(false);
        adapter = new ProcessProAdapter(this, dataList);
        binding.processRecyclerView.setAdapter(adapter);
        binding.processRecyclerView.addItemDecoration(new LeftAndRightTagDecoration(this));

    }


    /**
     * 获取数据
     */
    public void getData() {

        OkHttpUtils
                .get()
                .url(ApiConfig.MY_PROCESS_SCHEDULE)
                .addParams("contractCode", getIntent().getStringExtra("contractCode"))
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (DataFilter.filter(ProcessProgressActivity.this, response, null)) {
                            //数据列表
                            ArrayList<Map<String, Object>> list = JSON.parseObject(response.get("data").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                            });
                            adapter.setData(list);
                        }
                    }
                });
    }
}
